package com.weaver.qfengx.action;

import com.engine.common.util.ServiceUtil;
import com.engine.workflow.entity.publicApi.PAResponseEntity;
import com.engine.workflow.entity.publicApi.ReqOperateRequestEntity;
import com.engine.workflow.publicApi.WorkflowRequestOperatePA;
import com.engine.workflow.publicApi.impl.WorkflowRequestOperatePAImpl;
import com.weaver.qfengx.NumberUtils;
import com.weaver.qfengx.StringUtils;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

public class WorkflowForceOverAction extends BaseAction {

    public WorkflowForceOverAction() {
        setActionName("公共Action之指定流程强制归档");
    }

    private String cancelRequest;

    public String getCancelRequest() {
        return cancelRequest;
    }

    public WorkflowForceOverAction setCancelRequest(String cancelRequest) {
        this.cancelRequest = cancelRequest;
        return this;
    }

    @Override
    public void execCode(RequestInfo requestInfo) {
        log.writeLog("参数 cancelRequest => " + cancelRequest);
        if (StringUtils.isEmpty(cancelRequest)) {
            message = "未配置参数：cancelRequest";
            log.writeLog(message);
            isOk = false;
            return;
        }
        String formvalue = mainTableMap.get(cancelRequest);
        log.writeLog("cancelRequest formvalue => " + formvalue);
        int cancelRequestid = NumberUtils.parseInt(formvalue);
        if (cancelRequestid <= 0) {
            message = "欲取消流程请求ID非法";
            log.writeLog(message);
            isOk = false;
            return;
        }
        // 使用系统管理员身份进行流程强制归档操作
        User user = User.getUser(1, 0);//系统管理员
        ReqOperateRequestEntity reqOperateRequestEntity = new ReqOperateRequestEntity();
        reqOperateRequestEntity.setRequestId(cancelRequestid);//被调用的requestid
        Map<String, Object> otherparams = new HashMap<String, Object>();
        otherparams.put("ismonitor", 1);
        reqOperateRequestEntity.setOtherParams(otherparams);
//        Timer timer = new Timer();
//        timer.schedule(new HitachidoForceOverAction.Task(user, reqOperateRequestEntity), secondes * 1000);
        WorkflowRequestOperatePA workflowRequestOperatePA = (WorkflowRequestOperatePAImpl) ServiceUtil.getService(WorkflowRequestOperatePAImpl.class);
        PAResponseEntity paResponseEntity = workflowRequestOperatePA.doForceOver(user, reqOperateRequestEntity); //调用强制归档
        log.writeLog("强制归档返回状态 ===> " + paResponseEntity.getCode());
        if (!StringUtils.equals("SUCCESS", paResponseEntity.getCode())) {
            message = "流程请求ID：" + formvalue + "强制归档失败, code: " + paResponseEntity.getCode();
            log.writeLog(maintable);
            isOk = false;
        }
    }

}
